import Immutable from 'immutable'
import DB from '../utils/storage'

/**
 * 用户，这里只写了登陆接口返回的一部分属性，以后用到其他属性再补充
 * @type {[type]}
 * @property {String} loginName 登录账号，我们自己保存下来的
 */
const UserRecord = Immutable.Record({
  loginName: null,
  userId: null,
  epayAccount: null,
  mobile: null,
  email: null,
  userCode: null
}, 'User')

class User extends UserRecord {
  /**
   * 保存user到localstorage
   * @return {[type]} [description]
   */
  save () {
    DB.put('user', this.toJS())
  }

  /**
   * 从localstorage更新user
   * @return {User} 新user
   */
  async update () {
    const localUser = await DB.get('user')
    return this.merge(localUser || {})
  }

  /**
   * 删除本地user
   * @return {[type]} [description]
   */
  static delete () {
    DB.remove('user')
  }
}

export default User
